package com.tcm.doctor.web.controller;

import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.utils.R;
import com.search.service.SearchItemService;
import com.search.service.dto.SearchItem;
import com.search.service.dto.SearchItemParam;
import com.tcm.item.domain.dto.DrugsDTO;
import com.tcm.item.service.drugs.DrugsService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Api(value = "drugs", tags = "药材接口")
@RequestMapping("/doctor")
@RestController
public class DrugsController {

    @DubboReference(version = "0.0.1-alpha", timeout = 3000, check = false, retries = 3)
    private DrugsService drugsService;

    @DubboReference(version = "0.0.1-alpha", timeout = 3000, check = false, retries = 3)
    private SearchItemService searchItemService;

    /**
     * 根据药房id，搜索关键词查询药品列表
     * @return
     * @throws Exception
     */
    @GetMapping("/list/drugs/page")
    public R listPage(@RequestParam Long storeId,
                      @RequestParam(defaultValue = "0") Long page,
                      @RequestParam(defaultValue = "10") Long pagesize) throws Exception {

        PageBean<DrugsDTO> drugsVoPageBean = drugsService.listDrugsPageByStoreId(storeId, page, pagesize);
        return R.success(drugsVoPageBean);
    }

    /**
     * 根据药房id，搜索关键词查询药品列表
     * @return
     * @throws Exception
     */
    @GetMapping("/list/search/drugs/page")
    public R listSearchPage(@RequestParam Long storeId,
                      @RequestParam(defaultValue = "") String name,
                      @RequestParam(defaultValue = "1") int page,
                      @RequestParam(defaultValue = "10") int pagesize) throws Exception {
        SearchItemParam searchItemParam = new SearchItemParam();
        searchItemParam.setName(name);
        searchItemParam.setStoreId(storeId);
        PageBean<SearchItem> drugsVoPageBean = searchItemService.search(searchItemParam, page, pagesize, "-1");
        return R.success(drugsVoPageBean);
    }

    /**
     * 新增药品
     * @param drugs
     * @return
     */
    @PostMapping("/drugs/save")
    public R save(@RequestBody DrugsDTO drugs) throws ServiceException{
        try{
            drugsService.save(drugs);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 更新药品
     * @param drugsDto
     * @return
     */
    @PostMapping("/drugs/update")
    public R update(@RequestBody DrugsDTO drugsDto) throws ServiceException{
        try{
            drugsService.update(drugsDto);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 删除药品
     * @param id
     * @return
     */
    @GetMapping("/drugs/delete")
    public R delete(@RequestParam("id") Long id) throws ServiceException {
        try{
            drugsService.deleteLogicById(id);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }
}
